﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit;
using System.Xml;
namespace DotNetUI.Controls
{
    /// <summary>
    /// 代码查看器
    /// </summary>
   
    public class DotNetCodeViewer : TextEditor
    {
        static DotNetCodeViewer()
        {
            HightLights.Add(Langugage.Aspx, "ICSharpCode.AvalonEdit.Highlighting.Resources.ASPX.xshd");
            HightLights.Add(Langugage.Json, "ICSharpCode.AvalonEdit.Highlighting.Resources.Json.xshd");
            HightLights.Add(Langugage.XmlDoc, "harpCode.AvalonEdit.Highlighting.Resources.XmlDoc.xshd");
            HightLights.Add(Langugage.Xml, "ICSharpCode.AvalonEdit.Highlighting.Resources.XML-Mode.xshd");
            HightLights.Add(Langugage.VB, "ICSharpCode.AvalonEdit.Highlighting.Resources.VB-Mode.xshd");
            HightLights.Add(Langugage.TSQL, "ICSharpCode.AvalonEdit.Highlighting.Resources.TSQL-Mode.xshd");
            HightLights.Add(Langugage.Tex, "ICSharpCode.AvalonEdit.Highlighting.Resources.Tex-Mode.xshd");
            HightLights.Add(Langugage.Python, "ICSharpCode.AvalonEdit.Highlighting.Resources.Python-Mode.xshd");
            HightLights.Add(Langugage.PowerShell, "ICSharpCode.AvalonEdit.Highlighting.Resources.PowerShell.xshd");
            HightLights.Add(Langugage.PHP, "ICSharpCode.AvalonEdit.Highlighting.Resources.PHP-Mode.xshd");
            HightLights.Add(Langugage.Path, "ICSharpCode.AvalonEdit.Highlighting.Resources.Patch-Mode.xshd");
            HightLights.Add(Langugage.ModeV2, "ICSharpCode.AvalonEdit.Highlighting.Resources.ModeV2.xshd");
            HightLights.Add(Langugage.ModeV1, "ICSharpCode.AvalonEdit.Highlighting.Resources.ModeV1.xshd");
            HightLights.Add(Langugage.MarkDownWithFontSize, "ICSharpCode.AvalonEdit.Highlighting.Resources.MarkDownWithFontSize.xshd");
            HightLights.Add(Langugage.MarkDown, "ICSharpCode.AvalonEdit.Highlighting.Resources.MarkDown-Mode.xshd");
            HightLights.Add(Langugage.JAVA, "ICSharpCode.AvalonEdit.Highlighting.Resources.JavaScript-Mode.xshd");
            HightLights.Add(Langugage.HTML, "ICSharpCode.AvalonEdit.Highlighting.Resources.HTML-Mode.xshd");
            HightLights.Add(Langugage.CSS, "ICSharpCode.AvalonEdit.Highlighting.Resources.CSS-Mode.xshd");
            HightLights.Add(Langugage.CSharp, "ICSharpCode.AvalonEdit.Highlighting.Resources.CSharp-Mode.xshd");
            HightLights.Add(Langugage.Cpp, "ICSharpCode.AvalonEdit.Highlighting.Resources.CPP-Mode.xshd");
            HightLights.Add(Langugage.Coco, "ICSharpCode.AvalonEdit.Highlighting.Resources.Coco-Mode.xshd");
            HightLights.Add(Langugage.Boo, "ICSharpCode.AvalonEdit.Highlighting.Resources.Boo.xshd");
            
        }

        public static readonly DependencyProperty CodeProperty = DependencyProperty.Register(nameof(Code), typeof(string), typeof(DotNetCodeViewer), new PropertyMetadata(string.Empty, (s, e) =>
        {
            if (s is DotNetCodeViewer view)
            {
                view.Text=e.NewValue?.ToString();
            }
        }));
        /// <summary>
        /// 显示的字符串值
        /// </summary>
        public string Code { get => this.GetValue<string>(CodeProperty); set => SetValue(CodeProperty, value); }


        public async void SetLangugage(Langugage langugage)
        {
            this.SyntaxHighlighting = LoadHighlightingDefinition(HightLights[langugage]);
        }
        private IHighlightingDefinition LoadHighlightingDefinition(string resourceName)
        {

            var assembly = typeof(HighlightingLoader).Assembly;
            using (System.IO.Stream s = assembly.GetManifestResourceStream(resourceName))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    var xshd = HighlightingLoader.LoadXshd(reader);
                    return HighlightingLoader.Load(xshd, HighlightingManager.Instance);
                }
            }
        }
        private static readonly Dictionary<Langugage, string> HightLights = new Dictionary<Langugage, string>();
    }
}
